//
//  JiaoYiSearchTabTableViewCell.m
//  DBAChain
//
//  Created by Evan on 2018/9/6.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "JiaoYiSearchTabTableViewCell.h"
#import "CommonData.h"
#import <Masonry.h>

@implementation JiaoYiSearchTabTableViewCell{
    CGFloat tab1xx;
    CGFloat tab2xx;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(void)initUI{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self segmCheck];
    [self segmCheckIMG];
}


#pragma mark - Segm

-(UIImageView*)segmCheckIMG{
    if (!_segmCheckIMG) {
        _segmCheckIMG = [[UIImageView alloc]init];
        [self addSubview:_segmCheckIMG];
        tab1xx = ((KScreenWidth-30)/2-60)/2 + 15;
        tab2xx = (KScreenWidth-30)/2+ tab1xx;
        _segmCheckIMG.frame = CGRectMake(tab1xx, 45, 60, 2);
        _segmCheckIMG.image = [UIImage imageNamed:@"矩形Line"];
    }
    return _segmCheckIMG;
}

-(UISegmentedControl*)segmCheck{
    if (!_segmCheck) {
        _segmCheck = [[UISegmentedControl alloc]initWithItems:@[@"求购列表",@"出售列表"]];
        [self addSubview:_segmCheck];
        [_segmCheck mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self);
            make.top.mas_equalTo(self).offset(10);
            make.height.offset(40);
            make.width.mas_equalTo(self).offset(-30);
        }];
        _segmCheck.tintColor = [UIColor clearColor];
        NSDictionary *selected = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:LCFromRGB(240, 92, 164)};
        //定义未选中状态下的样式normal，类型为字典
        NSDictionary *normal = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:LCFromRGB(51, 51, 51)};
        [_segmCheck setTitleTextAttributes:normal forState:UIControlStateNormal];
        [_segmCheck setTitleTextAttributes:selected forState:UIControlStateSelected];
        
        _segmCheck.selectedSegmentIndex = 0;
        [_segmCheck addTarget:self action:@selector(segmentValueChanged:)forControlEvents:UIControlEventValueChanged];
    }
    return _segmCheck;
}

-(void)segmentValueChanged:(UISegmentedControl*)sender{
    if(sender.selectedSegmentIndex == 0){
        [UIView animateWithDuration:0.3 animations:^{
            self.segmCheckIMG.frame = CGRectMake(self->tab1xx, 45, 60, 2);
        }];
    }else{
        [UIView animateWithDuration:0.3 animations:^{
            self.segmCheckIMG.frame = CGRectMake(self->tab2xx, 45, 60, 2);
        }];
    }
}

@end
